﻿
function populateGadgets()
{
    if (!tab_gadgets_loaded)
        show_wait("gadgets_tab");
    osapi.http.get({
        'href': get_rest_apps_self(),
        'format': 'json',
        'oauth_service_name': oauth_service_name,
        'authz': authz_type
    }).execute(fill_gadgets);
}

function fill_gadgets(data)
{
    show_errors_if_any(data);
    //saving hash in order to know when new data arrives, 
    //if the hash is the same when the tab is refreshed - no need to redraw it
    if (compare_and_save_hash(data.metadata.DataHash))
        return;
    show_wait("gadgets_tab");

    var tab_container = $("gadgets_tab_container");

    remove_all_children(tab_container);

    // container for retrieved data
    var container = new Element('div');
    container.addClassName('contentbox');
    tab_container.appendChild(container);
    var i = 0;
    while (i < data.content.totalResults)
    {
        if (data.content.entry[i])
        {
            var item = new Element('div');
            item.addClassName('activity_mini');
            container.appendChild(item);

            var details = new Element('div');

            var name = new Element('div');
            name.update(data.content.entry[i].name);
            name.addClassName('item_name');
            details.appendChild(name);

            var description = new Element('div');
            description.update(data.content.entry[i].description);
            description.addClassName('item_description');
            details.appendChild(description);

            var group_type = new Element('div');
            group_type.update(data.content.entry[i].gadget_url);
            group_type.addClassName('item_type');
            details.appendChild(group_type);

            item.appendChild(details);
        }
        i++;
    }
    hide_wait("gadgets_tab");
    tab_gadgets_loaded = true;
}

function update_gadgets()
{
    $('update_image').setAttribute('src', update_image_moving_url);
    populateGadgets();
    $('update_image').setAttribute('src', update_image_url);
}